package com.ddxz.tool.core.crypto.codec;

import static com.ddxz.tool.core.crypto.codec.CryptoConstData.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/15
 */

public class BCD {

    /**
     * 字符串转BCD码
     *
     * @param asc ASCII字符串
     * @return BCD
     */
    public static byte[] strToBcd(String asc) {
        int len = asc.length();
        int mod = len % TWO;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte[] abt;
        if (len >= TWO) {
            len >>= 1;
        }
        byte[] bbt;
        bbt = new byte[len];
        abt = asc.getBytes();
        int j;
        int k;
        for (int p = 0; p < asc.length() / TWO; p++) {
            if ((abt[TWO * p] >= '0') && (abt[TWO * p] <= '9')) {
                j = abt[TWO * p] - '0';
            } else if ((abt[TWO * p] >= 'a') && (abt[TWO * p] <= 'z')) {
                j = abt[TWO * p] - 'a' + 0x0a;
            } else {
                j = abt[TWO * p] - 'A' + 0x0a;
            }
            if ((abt[TWO * p + 1] >= '0') && (abt[TWO * p + 1] <= '9')) {
                k = abt[TWO * p + 1] - '0';
            } else if ((abt[TWO * p + 1] >= 'a') && (abt[TWO * p + 1] <= 'z')) {
                k = abt[TWO * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[TWO * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    /**
     * ASCII转BCD
     *
     * @param ascii ASCII byte数组
     * @return BCD
     */
    public static byte[] ascToBcd(byte[] ascii) {
        return ascToBcd(ascii, ascii.length);
    }

    /**
     * ASCII转BCD
     *
     * @param ascii     ASCII byte数组
     * @param ascLength 长度
     * @return BCD
     */
    public static byte[] ascToBcd(byte[] ascii, int ascLength) {
        byte[] bcd = new byte[ascLength / TWO];
        int j = 0;
        for (int i = 0; i < (ascLength + 1) / TWO; i++) {
            bcd[i] = ascToBcd(ascii[j++]);
            bcd[i] = (byte) (((j >= ascLength) ? 0x00 : ascToBcd(ascii[j++])) + (bcd[i] << 4));
        }
        return bcd;
    }

    /**
     * BCD转ASCII字符串
     *
     * @param bytes BCD byte数组
     * @return ASCII字符串
     */
    public static String bcdToStr(byte[] bytes) {
        char[] temp = new char[bytes.length * 2];
        char val;

        for (int i = 0; i < bytes.length; i++) {
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
            temp[i * TWO] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

            val = (char) (bytes[i] & 0x0f);
            temp[i * TWO + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
        }
        return new String(temp);
    }


    //----------------------------------------------------------------- Private method start

    /**
     * 转换单个byte为BCD
     *
     * @param asc ACSII
     * @return BCD
     */
    private static byte ascToBcd(byte asc) {
        byte bcd;

        if ((asc >= NUM_BEGIN) && (asc <= NUM_END)) {
            bcd = (byte) (asc - '0');
        } else if ((asc >= HEX_UPPER_BEGIN) && (asc <= HEX_UPPER_END)) {
            bcd = (byte) (asc - HEX_UPPER_BEGIN + 10);
        } else if ((asc >= HEX_LOWER_BEGIN) && (asc <= HEX_LOWER_END)) {
            bcd = (byte) (asc - HEX_LOWER_BEGIN + 10);
        } else {
            bcd = (byte) (asc - 48);
        }
        return bcd;
    }
    //----------------------------------------------------------------- Private method end

}
